function [M, geoLabels] = loadDataUser1stTimeActivities(platformDataFolder)
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	% This function reads four input files:
	%	- userData.csv
	%	- providerData.csv
	%	- evaluationsData.csv
	% 
	% It output a table that gives, for each user account created (in userData.csv, the incomplete ones without locations have already been dropped),
	% the timestamp of their account creation, first car creation, "provider exit date" (moment at which they had no more cars posted, for those who had one)
	% and the date of first transaction as consumer.
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%%%% Inputs:
	% 
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%%%% Outputs:
	% M:			table
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

geoAggLevel = 'CantonId';

% Define userDataFile, providerDataFile and inputTransactionFile (output by data_cleaning)
userDataFile = sprintf('%s/userData.csv', platformDataFolder);
providerDataFile = sprintf('%s/providerData.csv', platformDataFolder);
inputTransactionFile = sprintf('%s/evaluationsData.csv', platformDataFolder);
geoDefFile     = sprintf('%s/../covariates/Scope_CantonId.csv', platformDataFolder);


% Read geo-def file to get geoLabels
opts_geo = detectImportOptions(geoDefFile);
opts_geo = setvartype(opts_geo, 'CantonId', 'char');
Mgeo = readtable(geoDefFile, opts_geo);
geoLabels = unique(table2cell(Mgeo(:,1)));
K = length(geoLabels);


%%% Read user data + convert location to integer kk + get unique userIds
opts = detectImportOptions(userDataFile); opts = setvartype(opts, geoAggLevel, 'char');
userData = readtable(userDataFile, opts);
userData = userData(:,{'UserId', 'AccountCreationDate_julian', geoAggLevel});
clear opts;
userIds = table2array(userData(:,'UserId'));
NumUsers = length(userIds);


[flag, user2geoIdx] = ismember(table2cell(userData(:,geoAggLevel)), geoLabels);
assert(all(flag));
M_user_kk = table(user2geoIdx); M_user_kk.Properties.VariableNames = {'kk'};
userData = [userData M_user_kk];
userData = userData(:,[1 2 4]);
clear flag user2geoIdx M_user_kk;

%%% Read providerData
opts = detectImportOptions(providerDataFile); opts = setvartype(opts, geoAggLevel, 'char');
providerData = readtable(providerDataFile, opts);
providerData = providerData(:,{'UserId', 'providerCreationDate_julian', 'providerDeletionDate_julian'});
clear opts myUserIds flag myUserIdxes myTable;

%%% Read transactionData + determine date of first transaction for each user who played the role of consumer at least once
% Read transactionsData file
transactionsData = readtable(inputTransactionFile);

consumerData = transactionsData(:,{'eventDate_julian', 'ConsumerId', 'ProviderId'});
a = table2array(consumerData(:,'eventDate_julian')); assert(all(a(2:end) >= a(1:end-1))); clear a; % Check that transactions are ordered by incr. date.
% Retain first ransaction for each consumer
myUserIds = table2array(consumerData(:,{'ConsumerId'}));
myUnqUserIds = unique(myUserIds);
[flag,myUserIdxes] = ismember(myUserIds, myUnqUserIds);
assert(all(flag));
a = accumarray(myUserIdxes, [1:length(myUserIdxes)]', [length(myUnqUserIds) 1], @min);
consumerData = consumerData(a,[2 1 3]);
consumerData.Properties.VariableNames = {'UserId', 'Consumer1stTransacDate_julian', 'Consumer1stTransac_idOfProvider'};
% Get location of the provider met by the user for his first transaction as consumer
tmpProviderIds = table2array(consumerData(:,'Consumer1stTransac_idOfProvider'));
[flag2,tmpProviderIds_userIdxes] = ismember(tmpProviderIds, userIds);
assert(all(flag2)); clear flag2;
Consumer1stTransac_carLocation_k2 = table2array(userData(tmpProviderIds_userIdxes,'kk'));
consumerData = [consumerData table(Consumer1stTransac_carLocation_k2)];
consumerData = consumerData(:,{'UserId', 'Consumer1stTransacDate_julian', 'Consumer1stTransac_carLocation_k2'});
% Clean up
clear myUserIds myUnqUserIds flag myUserIdxes a tmpProviderIds tmpProviderIds_userIdxes Consumer1stTransac_carLocation_k2;

% Retain first transaction for each provider
providerTransacData = transactionsData(:,{'eventDate_julian', 'ProviderId'});
a = table2array(providerTransacData(:,'eventDate_julian')); assert(all(a(2:end) >= a(1:end-1))); clear a; % Check that transactions are ordered by incr. date.
% Retain first transaction for each provider
myUserIds = table2array(providerTransacData(:,{'ProviderId'}));
myUnqUserIds = unique(myUserIds);
[flag,myUserIdxes] = ismember(myUserIds, myUnqUserIds);
assert(all(flag));
a = accumarray(myUserIdxes, [1:length(myUserIdxes)]', [length(myUnqUserIds) 1], @min);
providerTransacData = providerTransacData(a,[2 1]);
providerTransacData.Properties.VariableNames = {'UserId', 'Provider1stTransacDate_julian'};
clear myUserIds myUnqUserIds flag myUserIdxes a ;

clear transactionsData;


%%% Merge userData, providerData, consumerData
M = outerjoin(userData, consumerData);
M.Properties.VariableNames{1} = 'UserId';
M = outerjoin(M, providerTransacData);
M.Properties.VariableNames{1} = 'UserId';
M = outerjoin(M, providerData);
M.Properties.VariableNames{1} = 'UserId';
M = M(:, {'UserId', 'AccountCreationDate_julian', 'kk', 'Consumer1stTransacDate_julian', 'Provider1stTransacDate_julian', 'providerCreationDate_julian', 'providerDeletionDate_julian'});

% Categorize users based on first behavior (consumer transaction or "providerCreationDate_julian")
userType = zeros(NumUsers, 1);
consumer1stDate = table2array(M(:,'Consumer1stTransacDate_julian'));
provider1stDate  = table2array(M(:,'providerCreationDate_julian'));
flag1 = isnan(consumer1stDate) & isnan(provider1stDate);						 % user with no recorded activity
flag2 = ~flag1 & (isnan(provider1stDate) | consumer1stDate < provider1stDate);	 % consumer
flag3 = ~flag1 & (isnan(consumer1stDate) | provider1stDate <= consumer1stDate); % provider
assert(all(flag1+flag2+flag3 == 1)); % Check that all users are set to one (and exactly one) user type
userType(flag1) = 1;
userType(flag2) = 2;
userType(flag3) = 3;
M = [M table(userType)];

end
